<template>
  <div class="modify-pwd-us-container">

    <hurdle :showLine="0"></hurdle>

    <banner bannerText="修改密码" bannerEn="CHANGE PASSWORD"></banner>

    <div class="modify-pwd-box">

      <div class="modify-pwd-con">

        <div class="modify-pwd-user-phone">
          <span class="user-input-text">旧密码：</span>
          <input type="password" placeholder="请输入旧登录密码" class="user-input-con" maxlength="16" v-model="oldPwd"/>
        </div>

        <div class="modify-pwd-user-pwd">
          <span class="user-input-text">新密码：</span>
          <input type="password" placeholder="请设置6-16位登录密码" class="user-input-con" maxlength="16" v-model="newPwd1"/>
        </div>

        <div class="modify-pwd-user-pwd">
          <span class="user-input-text">确认密码：</span>
          <input type="password" placeholder="请再次输入新密码" class="user-input-con" maxlength="16" v-model="newPwd2"/>
        </div>

        <button type="button" class="modify-pwd-user-btn" @click="commitModify">确定</button>
      </div>
    </div>

    <service></service>

    <storeFooter></storeFooter>
  </div>
</template>

<script>
  import hurdle from "../../components/hurdle"
  import storeFooter from "../../components/storeFooter"
  import banner from "../../components/banner"
  import {checkPwd} from "../../config/verify";
  import {Loading} from 'element-ui';
  import {baseUrl, salt} from "../../config/env";
  import md5 from 'js-md5';
  import {getSessionStore} from "../../config/util";
  import service from "../../components/service";

  export default {
    data() {
      return {
        oldPwd: "",
        newPwd1: "",
        newPwd2: "",
        userWand: "",
        userToken: ""
      }
    },
    created() {
      let base64 = require('js-base64').Base64;
      if (getSessionStore("userWand")) {
        this.userWand = base64.decode(getSessionStore("userWand"));
        this.userToken = getSessionStore("userToken")
      }
    },
    methods: {
      checkNewPwd(pwd) {//校验密码
        if (pwd == "" || pwd == undefined || pwd == null) {
          this.$message({message: "新密码不能为空", center: true, type: 'error', duration: 1000});
          return;
        } else if (pwd.indexOf(" ") >= 0) {
          this.$message({message: "新密码中不能有空格字符", center: true, type: 'error', duration: 1000});
          return;
        } else if ((/[\u4e00-\u9fa5]+/).test(pwd)) {
          this.$message({message: "新密码不能包含中文", center: true, type: 'error', duration: 1000});
          return;
        } else if (pwd.length < 6 || pwd.length > 16) {
          this.$message({message: "新密码长度为6-16位", center: true, type: 'error', duration: 1000});
          return;
        }
        return true;
      },
      checkConfirmPwd(newPwd, pwd) {
        if (newPwd == "" || pwd == undefined || pwd == null) {
          this.$message({message: "确认密码不能为空", center: true, type: 'error', duration: 1000});
          return;
        } else if (newPwd.indexOf(" ") >= 0) {
          this.$message({message: "确认密码中不能有空格字符", center: true, type: 'error', duration: 1000});
          return;
        } else if ((/[\u4e00-\u9fa5]+/).test(newPwd)) {
          this.$message({message: "确认密码不能包含中文", center: true, type: 'error', duration: 1000});
          return;
        } else if (newPwd.length < 6 || newPwd.length > 16) {
          this.$message({message: "确认密码长度为6-16位", center: true, type: 'error', duration: 1000});
          return;
        } else if (newPwd != pwd) {
          this.$message({message: "两次密码不一致", center: true, type: 'error', duration: 1000});
          return;
        }
        return true;
      },
      commitModify() {
        if (checkPwd(this.oldPwd, 2) && this.checkNewPwd(this.newPwd1) && this.checkConfirmPwd(this.newPwd1, this.newPwd2)) {
          let loading = Loading.service({
            text: '拼命加载中....',
            lock: false,
            background: 'rgba(0, 0, 0, 0.7)'
          });

          this.axios.get(baseUrl + "/login/modify_psw?userToken=" + this.userToken + "&newPsw=" + md5(md5(this.newPwd1) + salt) + "&password=" + md5(md5(this.oldPwd) + salt)).then((res) => {
            loading.close();
            let retObj = res.data;
            if (retObj.code == 0) {
              if (retObj.data == 0) {
                this.$message({message: "密码修改失败，请重试", center: true, type: 'error', duration: 1000});
              } else {
                this.$router.push('/modify-pwd-success');
              }
            } else {
              this.$message({message: retObj.msg, center: true, type: 'error', duration: 1000});
            }
          });
        }
      }
    },
    components: {
      hurdle,
      storeFooter,
      banner,
      service
    }
  }
</script>

<style lang="less" scoped>
  @import "../../assets/mixin";

  .modify-pwd-us-container {
    .wh(100%, auto);
    .modify-pwd-box {
      .wh(100%, auto);
      display: flex;
      justify-content: center;
      position: relative;
      .modify-pwd-con {
        padding-top: 65px;
        padding-bottom: 60px;
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 464px;
        .modify-pwd-user-phone {
          display: flex;
          align-items: center;
          padding-bottom: 36px;
        }
        .modify-pwd-user-pwd {
          display: flex;
          align-items: center;
          padding-bottom: 28px;
        }
        .user-input-text {
          width: 90px;
          text-align: right;
          .size-color(18px, #4D4D4D);
        }
        .user-input-con {
          .wh(342px, 48px);
          background: rgba(245, 245, 245, 1);
          border: 1px solid rgba(235, 235, 235, 1);
          border-radius: 4px;
          padding-left: 18px;
        }
        .modify-pwd-user-btn {
          .wh(180px, 48px);
          background: #3CAFFF;
          border-radius: 4px;
          .size-color(16px, #FFFFFF);
          text-align: center;
          line-height: 48px;
          &:hover {
            cursor: pointer;
            background: #39A2ED;
          }
        }
      }
      .modify-pwd-server-pic {
        position: absolute;
        bottom: 20px;
        right: 20px;
        .wh(72px, 72px);
        .server-pic {
          .wh(72px, 72px);
        }
      }
    }
  }
</style>
